package com.example.service.impl;

import com.example.mapper.CategoryMapper;
import com.example.pojo.Category;
import com.example.service.CategoryService;
import com.example.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author fantasy
 * @Date 2024/7/2 22:47
 **/

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category) {
        Map<String, Object> o = ThreadLocalUtil.get();
        Integer id = (Integer) o.get("id");
        category.setCreateUser(id);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.add(category);
    }

    /**
     * 获取类别列表的方法，根据ThreadLocal中的数据查询数据库。
     *
     * @return 返回一个Category对象的列表，列表中的每个元素代表一个类别。
     */
    @Override
    public List<Category> get() {
        // 从ThreadLocal中获取存储的对象，并将其转换为Map
        Map<String, Object> o = ThreadLocalUtil.get();

        // 根据Map中"id"对应的值（转换为整数）查询Category
        return categoryMapper.get(Integer.parseInt(o.get("id").toString()));
    }

    @Override
    public Category getDetail(Integer id) {
        return categoryMapper.getDetail(id);
    }

    @Override
    public void update(Category category) {
        categoryMapper.update(category);
    }
}
